package com.charity.service.impl;

import com.charity.domain.Donation;
import com.charity.mapper.DonationMapper;
import com.charity.service.IDonationService;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;

@Service
@Primary
public class DonationServiceImpl implements IDonationService {

    @Resource
    private DonationMapper donationMapper;

    @Override
    public boolean createDonation(Donation donation) {
        return donationMapper.insert(donation) > 0;
    }

    @Override
    public Donation getDonationById(Long donationId) {
        return donationMapper.selectById(donationId);
    }

    @Override
    public List<Donation> getUserDonations(Long userId, int pageNum, int pageSize) {
        int offset = (pageNum - 1) * pageSize;
        return donationMapper.selectByUserId(userId, offset, pageSize);
    }

    @Override
    public List<Donation> getProjectDonations(Long projectId, int pageNum, int pageSize) {
        int offset = (pageNum - 1) * pageSize;
        return donationMapper.selectByProjectId(projectId, offset, pageSize);
    }

    @Override
    public double getProjectTotalAmount(Long projectId) {
        Double amount = donationMapper.selectProjectTotalAmount(projectId);
        return amount != null ? amount : 0.0;
    }

    @Override
    public double getUserTotalAmount(Long userId) {
        Double amount = donationMapper.selectUserTotalAmount(userId);
        return amount != null ? amount : 0.0;
    }
}